﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DZEROPhysicsResultsPages;
using t_TestingLibrary;
using DeepZoomPivotConstructor.PivotLib;
using PaperRenderingInterfaces;
using System.ComponentModel.Composition;

namespace t_DZEROPhysicsResultsPages
{
    [TestClass]
    public class t_ScanDZERONotePage
    {
        [TestInitialize]
        public void Setup()
        {
            Utilities.MyClassInit();
        }

        [TestCleanup]
        public void Cleanup()
        {
            Utilities.MyClassDone();
        }

        [TestMethod]
        public void TestCtor()
        {
            var t = new ScanDZERONotePage();
        }

        /// <summary>
        /// Helper class that doesn't actually do any rendering as much as hold onto things.
        /// </summary>
        [Export(typeof(IPaperRenderEngine))]
        class DummyRender : IPaperRenderEngine
        {
            public IEnumerable<DeepZoomPivotConstructor.PivotLib.PivotImage> Render(string renderName, string renderAddress)
            {
                PivotImage pm = new PivotImage(null);
                pm.Name = "jpeg";
                pm.Add("Graphics Files", "jpeg", new Uri("http://www-d0.fnal.gov/Run2Physics/WWW/results/prelim/B/B59/B59F01.jpeg"));
                yield return pm;
            }
        }

        [TestMethod]
        public void TestRenderDZEROPage()
        {
            var t = new ScanDZERONotePage();
            Utilities.AddPart(new DummyRender());
            Utilities.Compose(t);

            var results = t.Render("http://www-d0.fnal.gov/Run2Physics/WWW/results/prelim/B/B59/");

            var p = results.First();

            Assert.AreEqual(0, FindMetadata<PivotMetadataURL>(p, "More Information").Count(), "This render guy should not add more info right now - the dude we end calling dose that work...");
            Assert.AreEqual("B59F01", p.Name, "Name was not correctly re-written");
        }

        /// <summary>
        /// Hunt throught the metadata!
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <param name="pmf"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        private T1[] FindMetadata<T1>(PivotImage pmf, string p)
            where T1 : PivotMetadataBase
        {
            var md = from m in pmf
                     where m.Name == p && m.GetType() == typeof(T1)
                     let masType = (T1)m
                     where masType != null
                     select masType;
            return md.ToArray();
        }
    }
}
